package com.xiaobingby.admin.sys.controller;


import com.xiaobingby.admin.sys.dto.UserDetailsDto;
import com.xiaobingby.admin.sys.dto.UserDto;
import com.xiaobingby.admin.sys.entity.User;
import com.xiaobingby.admin.sys.service.IUserService;
import com.xiaobingby.common.controller.BaseController;
import com.xiaobingby.common.util.R;
import com.xiaobingby.common.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * sys_user 用户表 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2018-10-29
 */
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController<IUserService, User, Long> {

    @Autowired
    private IUserService iUserService;

    /**
     * 添加用户
     *
     * @param userDto
     * @return
     */
    @ResponseBody
    @PostMapping
    public R addUser(@RequestBody UserDto userDto) {
        iUserService.addUser(userDto);
        return R.ok();
    }

    /**
     * 更新用户
     *
     * @param userDto
     * @return
     */
    @ResponseBody
    @PutMapping
    public R updateUser(@RequestBody UserDto userDto) {
        iUserService.updateUser(userDto);
        return R.ok();
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @DeleteMapping("{ids}")
    public R delUser(@PathVariable Long[] ids) {
        boolean result = iUserService.deleteUserRole(ids);
        return R.ok(result);
    }

    /**
     * 通过用户ID查询 用户角色关系数据
     *
     * @param id
     * @return
     */
    @GetMapping("/userRolesInfo/{id}")
    public R userRolesInfo(@PathVariable Integer id) {
        UserDto userRolesInfo = iUserService.findUserRolesInfo(id);
        return R.ok(userRolesInfo);
    }

    /**
     * 通过用户名查询用户信息
     *
     * @param username
     * @return
     */
    @RequestMapping("/loadUserByUsername/{username}")
    public R<UserDetailsDto> loadUserByUsername(@PathVariable(name = "username") String username) {
        UserDetailsDto userDetailsDto = iUserService.loadUserByUsername(username);
        if (userDetailsDto != null) {
            return new R<User>().ok(userDetailsDto);
        }
        return new R<User>().error("用戶不存在");
    }

    @GetMapping("/getInfo")
    public R getInfo() {
        UserDetailsDto userDetails = SecurityUtils.getUserDetails();
        User user = new User();
        user.setId(userDetails.getId());
        user.setUsername(userDetails.getUsername());
        user.setNickname(userDetails.getNickname());
        user.setAvatar("https://file.iviewui.com/dist/a0e88e83800f138b94d2414621bd9704.png");
        return R.ok(user);
    }

    @Autowired
    ResourceServerTokenServices resourceServerTokenServices;

    @GetMapping("/userInfo")
    public Object userInfo(@RequestParam("token") String value) {
        OAuth2Authentication oAuth2Authentication = resourceServerTokenServices.loadAuthentication(value);
        Object principal = oAuth2Authentication.getPrincipal();
        return oAuth2Authentication;
    }

}
